﻿using DatabaseMsg;
using DotNetClient;
using Frameworks;
using LobbyMsg;
using UnityEngine;
using UnityEngine.UI;

namespace Doudizhu
{
    public class DoudizhuMenu : MonoBehaviour
    {
        [Header("Menu")]
        public Button back_btn;

        [Header("User Info")]
        public Image avata_img;
        public Text name_txt;
        public Text gold_txt;
        public Text total_txt;
        public Text win_txt;
        public Text winRate_txt;

        [Header("Create Game")]
        public Toggle createGame_toggle;
        public GameObject createGame_panel;
        public Button match_btn;
        public Text matchBtn_txt;
        private bool _isMatching = false;

        [Header("Find Games")]
        public Toggle findGames_toggle;
        public GameObject findGames_panel;

        [Header("Join Game")]
        public Toggle joineGame_toggle;
        public GameObject joinGame_panel;

        private DoudizhuInfo _doudizhuInfo;

        private void Awake()
        {

        }

        private void Start()
        {
            createGame_toggle.onValueChanged.AddListener(setActiveCreateGamePanel);
            findGames_toggle.onValueChanged.AddListener(setActiveFindGamesPanel);
            joineGame_toggle.onValueChanged.AddListener(setActiveJoinGamePanel);

            match_btn.onClick.AddListener(matchBtnClick);

            EventProxy.AddEventListener(Routes.C2S_DB_GET_DOUDIZHU_INFO, onResGetDoudizhuInfo);
            EventProxy.AddEventListener(Routes.C2S_LOBBY_MATCH, onResMatch);

            name_txt.text = MyselManager.Instance.Myself.Nickname;
            getDoudizhuInfo();

            createGame_panel.SetActive(true);
            findGames_panel.SetActive(false);
            joinGame_panel.SetActive(false);
        }

        private void OnDestroy()
        {
            EventProxy.RemoveEventListener(Routes.C2S_DB_GET_DOUDIZHU_INFO, onResGetDoudizhuInfo);
            EventProxy.RemoveEventListener(Routes.C2S_LOBBY_MATCH, onResMatch);
        }

        // User Info
        private void updateUserInfo()
        {
            uint total = _doudizhuInfo.Win + _doudizhuInfo.Lose + _doudizhuInfo.Escape;
            float winRate = _doudizhuInfo.Win > 0 ? (float)total / _doudizhuInfo.Win : 0;

            gold_txt.text = _doudizhuInfo.Gold.ToString();
            total_txt.text = total.ToString();
            win_txt.text = _doudizhuInfo.Win.ToString();
            winRate_txt.text = winRate.ToString("0%");
        }

        private void getDoudizhuInfo()
        {
            c2s_getDoudizhuInfo user = new c2s_getDoudizhuInfo() { Uid = MyselManager.Instance.Myself.Id };
            NetworkManager.Instance.Request(Routes.C2S_DB_GET_DOUDIZHU_INFO, user);
        }

        private void onResGetDoudizhuInfo(EventData eventData)
        {
            if (eventData == null || eventData.target == null) return;

            _doudizhuInfo = ((s2c_getDoudizhuInfo)eventData.target).Info;
            updateUserInfo();
        }

        // Create Game
        private void setActiveCreateGamePanel(bool value)
        {
            createGame_panel.SetActive(value);
        }

        // Find Games
        private void setActiveFindGamesPanel(bool value)
        {
            findGames_panel.SetActive(value);
        }

        private void matchBtnClick()
        {
            if (_isMatching)
            {
                _isMatching = !_isMatching;
                matchBtn_txt.text = _isMatching ? "取消匹配" : "开始匹配";

                c2s_unmatch msg = new c2s_unmatch() { GameMode = (uint)MyselManager.Instance.GameMode };
                NetworkManager.Instance.Notify(Routes.C2S_LOBBY_UNMATCH, msg);
            }
            else
            {
                match_btn.interactable = false;

                c2s_match msg = new c2s_match() { GameMode = (uint)MyselManager.Instance.GameMode };
                NetworkManager.Instance.Request(Routes.C2S_LOBBY_MATCH, msg);
            }
        }

        private void onResMatch(EventData eventData)
        {
            match_btn.interactable = true;
            _isMatching = !_isMatching;
            matchBtn_txt.text = _isMatching ? "取消匹配" : "开始匹配";

            if (eventData == null || eventData.target == null) return;


        }

        // Join Game
        private void setActiveJoinGamePanel(bool value)
        {
            joinGame_panel.SetActive(value);
        }

    }
}
